$('.select2').select2();

function queryParams1() {
    alert($("#modelIdSelect").val());
    return {
        modelId: $("#modelIdSelect").val()
    }
}

function formulaFormatter(value, row, index) {
    if (checkIsNotNull(value)) {
        return "自动获取";
    } else {
        return "手动赋值";
    }
}


function entryTypeFormatter(value, row, index) {
    if (value == "1") {
        return "下拉菜单";
    } else if (value == "2") {
        return "输入框";
    } else {
        return value;
    }
}

function weightFormatter(value, row, index) {
    if ("2" == row.quotaTypeStyle) {
        return value;
    } else if (row.quotaTypeStyle = "1") {
        return (value * 100 ) + "%";
    } else if (row.quotaTypeStyle = "1") {
        if (value > 0) {
            return "+" + value;
        }
        return value;
    }
}

function descriptionFormatter(value, row, index) {
    if (value == "" || value == undefined)
        return value;
    if (value.length > 5) {
        return value.substr(0, 5) + "<a href=\"javascript:void(0)\" onclick=\"showTip('" + value + "')\"  \">......</a>";
    } else {
        return value;
    }
}

function modifyFormatter1(value, row, index) {
    return "<button type=\"button\" class=\"btn btn-block btn-primary btn-xs\" onclick=\"javascript:modifyQuota('1'," + index + ")\">修改</button>";
}

function modifyFormatter2(value, row, index) {
    return "<button type=\"button\" class=\"btn btn-block btn-primary btn-xs\" onclick=\"javascript:modifyQuota('2', " + index + ")\">修改</button>";
}


function modifyFormatter3(value, row, index) {
    return "<button type=\"button\" class=\"btn btn-block btn-primary btn-xs\" onclick=\"javascript:modifyQuota('3',  " + index + ")\">修改</button>";
}


function operateFormatter1(value, row, index) {
    return "<button type=\"button\" class=\"btn btn-block btn-primary btn-xs\" onclick=\"javascript:deleteQuota('1','table1','" + row.id + "')\">删除</button>";
}

function operateFormatter2(value, row, index) {
    return "<button type=\"button\" class=\"btn btn-block btn-primary btn-xs\" onclick=\"javascript:deleteQuota('2','table2','" + row.id + "')\">删除</button>";
}

function operateFormatter3(value, row, index) {
    return "<button type=\"button\" class=\"btn btn-block btn-primary btn-xs\" onclick=\"javascript:deleteQuota('3','table3','" + row.id + "')\">删除</button>";
}

function sortNumFormatter1(value, row, index) {
    if (index == 0) {
        return "<button type=\"button\" class=\"btn btn-primary btn-xs fa fa-arrow-down\" onclick=\"adjustSort('1','0','" + row.id + "','table1')\"></button>";
    }
    return "<button type=\"button\" class=\"btn btn-primary btn-xs fa fa-arrow-up\" onclick=\"adjustSort('1','1','" + row.id + "','table1')\"></button>";
}

function quotaTypeNameFormatter(value, row, index) {
    if (row.sql != null && row.sql != "" && row.sql != undefined) {
        return "("
    }
}

function sortNumFormatter2(value, row, index) {
    if (index == 0) {
        return "<button type=\"button\" class=\"btn btn-primary btn-xs fa fa-arrow-down\" onclick=\"adjustSort('1','0','" + row.id + "','table2')\"></button>";
    }
    return "<button type=\"button\" class=\"btn btn-primary btn-xs fa fa-arrow-up\" onclick=\"adjustSort('1','1','" + row.id + "','table2')\"></button>";
}

function sortNumFormatter3(value, row, index) {
    if (index == 0) {
        return "<button type=\"button\" class=\"btn btn-primary btn-xs fa fa-arrow-down\" onclick=\"adjustSort('2','0','" + row.id + "','table3')\"></button>";
    }
    return "<button type=\"button\" class=\"btn btn-primary btn-xs fa fa-arrow-up\" onclick=\"adjustSort('2','1','" + row.id + "','table3')\"></button>";
}

function adjustSort(type, isUp, id, tableId) {
    var url = "";
    if (type == "1") {
        url = "/quota/type/sort/adjust";
    } else {
        url = "/quota/value/sort/adjust";
    }
    var data = {
        isUp: isUp,
        id: id
    };
    postReq(url, data, function (result) {
        if (result.success) {
            refreshTable(tableId);
            toastr['success']('操作成功', '提示');
        } else {
            toastr['error'](result.msg, '提示');
        }
    });
}

function quotaValueStyleFormatter(value, row, index) {
    if (value == "1") {
        return "分数";
    } else if (value == "2") {
        return "公式";
    } else if (value == "3") {
        return "调整";
    }
}

function isVetoFormatter(value, row, index) {
    if (value == "1") {
        return "否决";
    } else if (value == "0") {
        return "不否决";
    }
}




function queryParams2() {
    var options = $("#table1").bootstrapTable("getSelections");
    if (options.length == 0) {
        return {};
    }
    return {
        modelDimensionId: options[0].id
    };
}

function queryParams3() {
    var options = $("#table2").bootstrapTable("getSelections");
    if (options.length == 0) {
        return {};
    }
    return {
        quotaTypeId: options[0].id
    };
}

function changeModel() {
    alert(111);
    alert(queryParams1().modelId);
    refreshTable('table1');
    $("#table2").bootstrapTable("removeAll");
    $("#table3").bootstrapTable("removeAll");
}

function refreshTable(id) {
    if ("table3" == id) {
        $("#" + id).bootstrapTable("refresh", {url: "/scoremodel/quotavalue/list"});
    } else if ("table2" == id) {
        $("#" + id).bootstrapTable("refresh", {url: "/scoremodel/quotatype/list"});
    } else if ("table1" == id) {
        $("#" + id).bootstrapTable("refresh", {url: "/scoremodel/dimension/list"});
    }
}

$('#table1').bootstrapTable({
    onCheck: function (row) {
        refreshTable('table2');
        $("#table3").bootstrapTable("removeAll");
    },
    onUncheck: function (row) {
        $("#table2").bootstrapTable("removeAll");
        $("#table3").bootstrapTable("removeAll");
    }
});
$('#table2').bootstrapTable({
    onCheck: function (row) {
        refreshTable('table3');
    },
    onUncheck: function (row) {
        $("#table3").bootstrapTable("removeAll");
    }
});

function addDimensionQuotaType() {
    alert(queryParams1()) ;
    var financialTime = $("#financialTimeSelect").val();
    var financialTimeName = $("#financialTimeSelect").find("option:selected").text();
    dimensionInit();
    $("#dimensionFinancialTimeName").val(financialTimeName);
    $("#dimensionFinancialTime").val(financialTime);
    $("#addDimensionModal").modal("show");
}

function addQuotaType() {
    var options = $("#table1").bootstrapTable("getSelections");
    if (options.length == 0) {
        toastr['error']("请点击维度列表选择维度", '提示');
        return;
    }
    var options = $("#table1").bootstrapTable("getSelections");
    specificInit(options[0]);
    $("#addSpecificQuotaTypeModal").modal("show");
}

function addQuotaValue() {
    var options = $("#table2").bootstrapTable("getSelections");
    if (options.length == 0) {
        toastr['error']("请点击具体指标列表选择具体指标", '提示');
        return;
    }
    var options = $("#table2").bootstrapTable("getSelections");
    quotaValueInit(options[0]);
    $("#addValueQuotaValueModal").modal("show");
}

function modifyQuota(type, index) {
    if (type == "1") {
        var financialTime = $("#financialTimeSelect").val();
        var financialTimeName = $("#financialTimeSelect").find("option:selected").text();
        dimensionInit();
        var options = $("#table1").bootstrapTable("getData");
        dimensionSetValue(options[index]);
        $("#dimensionFinancialTimeName").val(financialTimeName);
        $("#dimensionFinancialTime").val(financialTime);
        $("#addDimensionModal").modal("show");
    } else if (type == "2") {
        var options = $("#table1").bootstrapTable("getSelections");
        specificInit(options[0]);
        var options = $("#table2").bootstrapTable("getData");
        specificSetValue(options[index])
        $("#addSpecificQuotaTypeModal").modal("show");
    } else if (type == "3") {
        var options = $("#table2").bootstrapTable("getSelections");
        quotaValueInit(options[0]);
        var options = $("#table3").bootstrapTable("getData");
        quotaValueSetValue(options[index]);
        $("#addValueQuotaValueModal").modal("show");
    }
}

function deleteQuota(type, tableId, id) {
    var url;
    if (type == "1") {
        url = "/scoremodel/dimension/delete";
    } else if (type == "2") {
        url = "/scoremodel/quotatype/delete";
    } else if (type == "3") {
        url = "/scoremodel/quotavalue/delete";
    }
    var data = {id: id};
    postReq(url, data, function (result) {
        if (result.success) {
            toastr['success']('删除成功', '提示');
            refreshTable(tableId);
        } else {
            toastr['error'](result.msg, '提示');
        }
    });
}

function developing() {
    toastr['success']('开发中！！！！', '提示');
}

function checkModelIsComplete() {
    var industryCode = $("#industrySelect").val();
    var url = "/quota/model/check";
    var data = {id: industryCode};
    postReq(url, data, function (result) {
        if (result.success) {
            toastr['success']('模型完整', '提示');
        } else {
            toastr['error'](result.msg, '提示');
        }
    });
}